﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting.Messaging;

namespace AOUtils.Libraries
{
    public static class ResourceLoader
    {
        public static Image LoadImage(string filename)
        {
            return LoadImage(filename, @".\Resources\");
        }

        public static Image LoadImage(string filename, string path)
        {
            Image image = null;
            string fullpath = Path.Combine(path, filename);
            if (!File.Exists(fullpath))
                return null;

            using (Stream s = File.OpenRead(fullpath))
            {
                image = LoadImage(s);
            }
            return image;
        }

        public static Image LoadImage(Stream s)
        {
            return Image.FromStream(s);
        }
        public static T Load<T>(string filename) where T : ISerializable
        {
            return Load<T>(filename, @".\Resources\");
        }

        public static T Load<T>(string filename, string path) where T : ISerializable
        {
            string fullpath = Path.Combine(path, filename);
            if (!File.Exists(fullpath))
                return default(T);
            T obj = default(T);
            
            using (Stream s = File.Open(fullpath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                //obj = Load<T>(s);
            }
            return obj;
        }
    }
}
